package DataModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;


public class GUI extends DataModel{
        
    JFrame frame;
    JPanel panel;
    
    JLabel fNameLabel;
    JLabel lNameLabel;
    JLabel ageLabel;
    
    JTextField fNameText;
    JTextField lNameText;
    JTextField ageText;
    
    JButton next;
    JButton prev;
    JButton last;
    JButton first;
    
    JButton update;
    JButton delete;
    JButton add;
    JButton save;
    
    
    public GUI() {
    	frame();
    	btnAction();
    }
    
    public void frame(){
    	
    	frame = new JFrame();
    	panel = new JPanel();
    	
    	fNameLabel = new JLabel("First Name: ");
    	lNameLabel = new JLabel("Last Name: ");
    	ageLabel = new JLabel("Age: ");
    	
    	fNameText = new JTextField(10);
    	lNameText = new JTextField(10);
    	ageText = new JTextField(10);
    	
    	next = new JButton("Next");
    	prev = new JButton("Previous");
    	last = new JButton("Last");
    	first = new JButton("First");
    	
    	update = new JButton("Update");
    	delete = new JButton("Delete");
    	add = new JButton("Add");
    	save = new JButton("Save");
    	
    	panel.add(fNameLabel);
    	panel.add(fNameText);
    	panel.add(lNameLabel);
    	panel.add(lNameText);
    	panel.add(ageLabel);
    	panel.add(ageText);
    	
    	panel.add(next);
    	panel.add(prev);
    	panel.add(first);
    	panel.add(last);
    	
    	panel.add(update);
    	panel.add(delete);
    	panel.add(add);
    	panel.add(save);
    	
    	frame.add(panel);
    	//frame.pack();
    	frame.setVisible(true);
    	frame.setSize(600,400);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    	try{
    		rs.next();
    		
    		fNameText.setText(rs.getString("Fname"));
    		lNameText.setText(rs.getString("Lname"));
    		ageText.setText(rs.getString("age"));
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	
    }
    
    public void btnAction(){
    	
    	next.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				if(rs.next()){
    					fNameText.setText(rs.getString("Fname"));
    					lNameText.setText(rs.getString("Lname"));
    					ageText.setText(rs.getString("age"));
    				}else{
    					rs.previous();
    					JOptionPane.showMessageDialog(null, "No more records");
    				}
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	prev.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				if(rs.previous()){
    					fNameText.setText(rs.getString("Fname"));
    					lNameText.setText(rs.getString("Lname"));
    					ageText.setText(rs.getString("age"));
    				}else{
    					rs.next();
    					JOptionPane.showMessageDialog(null, "No more records");
    				}
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	
    	last.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				rs.last();
    				fNameText.setText(rs.getString("Fname"));
    				lNameText.setText(rs.getString("Lname"));
    				ageText.setText(rs.getString("age"));
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	first.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				rs.first();
    				fNameText.setText(rs.getString("Fname"));
    				lNameText.setText(rs.getString("Lname"));
    				ageText.setText(rs.getString("age"));
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	update.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				
    				String firstName = fNameText.getText();
    				String lastName = lNameText.getText();
    				String age = ageText.getText();
    				
    				rs.updateString("Fname", firstName);
    				rs.updateString("Lname", lastName);
    				rs.updateString("age", age);
    				rs.updateRow();
    				
    				JOptionPane.showMessageDialog(null, "record updated");
    				
    				
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	
    	delete.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				
    				rs.deleteRow();
    				//st.close();
    				//rs.close();
    				    				
    				st = con.createStatement(rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_UPDATABLE);
		    		String sql = "select * from Table1";
		    		rs = st.executeQuery(sql);
		    		
		    		rs.next();
		    		
		    		fNameText.setText(rs.getString("Fname"));
		    		lNameText.setText(rs.getString("Lname"));
		    		ageText.setText(rs.getString("age"));
    				
    				
    				
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	
    	add.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    
    			try{
    				fNameText.setText(" ");
    				lNameText.setText(" ");
    				ageText.setText(" ");  				
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	save.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent btn){
    			
    					String firstName = fNameText.getText();
    				  	String lastName = lNameText.getText();
    				  	String age = ageText.getText();	
    
    			try{
    				  	
    				  		
    				  	rs.moveToInsertRow();
    				  	
    				  	rs.updateString("Fname", firstName);
    				  	rs.updateString("Lname", lastName);
    				  	rs.updateString("age", age);
    				  	
    				  	rs.insertRow();
    				  	//st.close();
    				  	//rs.close();
    				  	
    				  	st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
		    			String sql = "select * from Table1";
		    			rs = st.executeQuery(sql);	
		    				
		    			rs.next();
		    			
		    			fNameText.setText(rs.getString("Fname"));
		    			lNameText.setText(rs.getString("Lname"));
		    			ageText.setText(rs.getString("age"));
		    				
    				
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	});
    	
    	
    }
    
}
